<template>
    <div>
        <el-dialog :title="obj.id ? '编辑' : '添加'" :visible.sync="visible" :before-close="conmel" width="500px">

            <el-form :model="obj" ref="form" :rules="rules">
                <el-form-item label="报废原因" label-width="120px" prop="scrapSource">
                    <el-input class="handle-input" v-model="obj.scrapSource" placeholder="请输入内容"></el-input>
                </el-form-item>
                <el-form-item label="排序" label-width="120px">
                    <el-input-number controls-position="right" :min="1" v-model="obj.sort"
                        autocomplete="off"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="conmel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { scrapadd, scrapedit } from "../../../../api/index"
export default {
    name: "reason_add_edit",
    props: {
        visible: {
            type: Boolean,
            required: true
        }
    },
    watch: {

    },
    components: {
    },
    data() {
        return {
            obj: {
                scrapSource: '',
                sort: 1
            },
            rules: {
                scrapSource: [
                    { required: true, message: '报废原因不能为空', trigger: 'blur' },
                ],
            }
        }
    },
    mounted() {

    },
    methods: {
        conmel() {
            this.$emit('close')
        },
        open(e) {
            this.obj = { ...e }

        },
        submit() {
            // console.log(this.form);
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    if (this.obj.id) {
                        await scrapedit(this.obj)
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                    } else {
                        await scrapadd(this.obj)
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                    }
                    //校验成功后执行事件
                    this.$emit('refresh')
                    // 关闭弹框
                    this.conmel()
                    // 清空表单
                    this.$refs.form.resetFields();


                } else {
                    return false;
                }
            });

        },
    }

}
</script>


<style lang="less" scoped>
::v-deep .handle-input {
    width: 200px;
}
</style>